package com.vcens.dingnotice.rest.v1;

import com.vcens.dingnotice.entity.TaskEntity;
import com.vcens.dingnotice.exception.RestException;
import com.vcens.dingnotice.service.impl.TaskerServiceImpl;
import com.vcens.dingnotice.utils.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author VeChar
 * @Desc 任务控制器
 * @CreateTime 2018/9/29 11:54 PM
 **/
@RestController
@Slf4j
@RequestMapping("/api/v1.0/task")
public class TaskController {
    @Autowired
    private TaskerServiceImpl taskerService;

    /**
     * 创建任务
     * 创建的时候仅仅把任务存放到数据库，然后在关联任务的时候才开始操作创建任务
     *
     * @param taskEntity
     * @return
     */
    @PostMapping("/createTask")
    public DataResult create(TaskEntity taskEntity) throws RestException {
        taskEntity.setId(1);
        taskEntity.setName("CreateTestRunner");
        taskEntity.setTaskGroup("CreateTestRunner");
        taskEntity.setTaskDesc("创建测试任务");
        taskEntity.setCronExpression("*/5 * * * * ?");
        taskEntity.setCreateTime(new Date());
        taskEntity.setUpdateTime(new Date());
        taskEntity.setTaskState(0);//默认未开启的状态，当只有在关联了执行任务之后再开启
        boolean isSuccess = taskerService.create(taskEntity);
        if (isSuccess) {
            return DataResult.ok();
        } else {
            return DataResult.build(500, "任务创建失败");
        }
//        taskerService.buildTask(taskEntity);
    }

    /**
     * 任务列表
     *
     * @param taskEntity
     * @param cp
     * @param pageSize
     * @return
     */
    @PostMapping("/list")
    public DataResult list(TaskEntity taskEntity, Integer cp, Integer pageSize) throws RestException {
//        List<TaskEntity> list = taskerService.listByPage(taskEntity, cp, pageSize);
        List<Map<String, Object>> list = taskerService.list();
        return DataResult.ok(list);
    }

    /**
     * 开始任务
     *
     * @param taskEntity
     * @return
     */
    @PostMapping("/trigger")
    public DataResult trigger(TaskEntity taskEntity) throws RestException {
        taskerService.triggerTask(taskEntity);
        return DataResult.ok();
    }

    /**
     * 暂停任务
     *
     * @param taskEntity
     * @return
     */
    @PostMapping("/pause")
    public DataResult pause(TaskEntity taskEntity) throws RestException {
        taskerService.pauseTask(taskEntity);
        return DataResult.ok();
    }

    /**
     * 恢复任务
     *
     * @param taskEntity
     * @return
     */
    @PostMapping("/resume")
    public DataResult resume(TaskEntity taskEntity) throws RestException {
        taskerService.resumeTask(taskEntity);
        return null;
    }

    /**
     * 删除任务
     *
     * @param taskEntity
     * @return
     */
    @PostMapping("/remove")
    public DataResult remove(TaskEntity taskEntity) throws RestException {
        taskerService.removeTask(taskEntity);

        return null;
    }
}
